﻿using Microsoft.AspNetCore.Mvc;
using Utility.Enum;
using DomainService.IService.OverallAuth;
using ApprovalFlow_WebApi.PlugInU;
using Microsoft.AspNetCore.Authorization;
using Model.BusinessModel.OutPut;
using Model.OverallAtuhModel;
using Utility;

namespace ApprovalFlow_WebApi.Controllers.OverallAuth
{
    /// <summary>
    /// OverallAuth系统接口
    /// ApprovalFlow系统的权限全部来自于overallAuth系统,分配也在overallAuth中分配
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.OverallAuthApi))]
    public class OverallAuthApiController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 用户服务
        /// </summary>
        public IOverallAuthApiService _overallAuthApiService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="overallAuthApiService"></param>
        public OverallAuthApiController(IOverallAuthApiService overallAuthApiService)
        {
            _overallAuthApiService = overallAuthApiService;
        }

        #endregion

        #region  公司相关

        /// <summary>
        /// 获取所有公司数据,select结构，登录使用
        /// </summary>
        /// <returns>以select结构，返回所有公司</returns>
        [HttpGet]
        [AllowAnonymous]
        public ReceiveStatus<List<SelectOutPut>> GetCorporation()
        {
            var receiveStatus = _overallAuthApiService.GetCorporation();
            return receiveStatus;
        }

        #endregion

        #region 菜单相关

        /// <summary>
        /// 获取用户所属菜单
        /// </summary>
        /// <returns>返回用户所属菜单</returns>
        [HttpGet]
        public ReceiveStatus<List<SysMenuOutPut>> GetMenuByUserId()
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetMenuByUserId(loginModel);
        }

        #endregion

        #region 按钮相关

        /// <summary>
        /// 获取菜单按钮权限
        /// </summary>
        /// <param name="menuId">菜单key</param>
        /// <param name="buttonRoleType">按钮类型</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<SysButtonDataOutPut>> GetMenuButtonAuthList(string menuId, int buttonRoleType)
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetMenuButtonAuthList(menuId, buttonRoleType, loginModel);
        }

        /// <summary>
        /// 获取数据按钮权限
        /// </summary>
        /// <param name="input">匹配模型</param>
        /// <returns></returns>
        [HttpPost]
        public ReceiveStatus<List<SysButtonDataOutPut>> GetMenuDataButtonAuth(MatchingDataButtonAuthInput input)
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetMenuDataButtonAuth(input, loginModel);
        }

        /// <summary>
        /// 获取按钮
        /// </summary>
        /// <param name="menuId">菜单key</param>
        /// <param name="buttonRoleType">按钮类型</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<SysButtonDataOutPut>> GetMenuButtonList(string menuId, int buttonRoleType)
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetMenuButtonList(menuId, buttonRoleType, loginModel.Token);
        }

        #endregion

        #region 用户相关

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="loginModel">登录模型</param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous] // 不验证权限
        public ReceiveStatus<List<LoginOutPut>> Login(LoginInput loginModel)
        {

            var result = _overallAuthApiService.Login(loginModel);
            if (result.success)
            {
                var loginResult = result.data?.First();
                var tokenResult = JwtPlugIn.BuildToken(loginResult);
                loginResult.Token = tokenResult.Token;
                loginResult.ExpiresDate = tokenResult.ExpiresDate;
                result.data = new List<LoginOutPut>() { loginResult };
            }

            return result;
        }

        /// <summary>
        /// 根据公司key,获取用户,用于登录
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public ReceiveStatus<List<SelectOutPut>> GetUserByCorporationKey(string corporationKey)
        {
            return _overallAuthApiService.GetUserByCorporationKey(corporationKey);
        }

        /// <summary>
        /// 根据公司获取所有用户
        /// 构建用户缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<SysUser>> GetAllUserByCorporationKey()
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetAllUserByCorporationKey(loginModel.CorporationKey);
        }
        #endregion

        #region  部门相关

        /// <summary>
        /// 根据公司获取所有部门数据
        /// 构建部门缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<SysDepartment>> GetAllDepartmentByCorporationKey()
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetAllDepartmentByCorporationKey(loginModel.CorporationKey);
        }

        /// <summary>
        /// 根据公司获取所有部门数据--树形结构
        /// 构建部门缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<TreeOutPut>> GetDepartmentTree()
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetDepartmentTree(loginModel.CorporationKey);
        }

        /// <summary>
        /// 获取部门下的人员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<TreeOutPut>> GetDepartmentUser()
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.GetDepartmentUser(loginModel.CorporationKey);
        }
        #endregion

        #region 数据列

        /// <summary>
        /// 根据菜单id获取列，用于数据列权限
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<List<SysMenuTableColsOutPut>> GetTableColsByMenuId(string menuId)
        {
            return _overallAuthApiService.GetTableColsByMenuId(menuId);
        }

        /// <summary>
        /// 同步数据列
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus SynchronousCols()
        {
            var loginModel = GetLoginUserMsg();
            return _overallAuthApiService.SynchronousCols(loginModel);
        }
        #endregion
    }
}
